package main

import (
	"flag"
	"fmt"
	"go-crontab/master"
	"runtime"
)

var (
	confFile string // 配置文件路径
)

func initArgs()  {
	// master -config ./master.json
	flag.StringVar(&confFile, "config", "./master.json", "请指定master.json文件路径")
	flag.Parse()
}

func initEnv() {
	runtime.GOMAXPROCS(runtime.NumCPU())
}

func main() {
	var (
		err error
	)

	initArgs()
	// Init goroutine num equals machine thread core num
	initEnv()
	// Init config
	if err = master.InitConfig(confFile); err != nil {
		goto ERR
	}

	if err = master.InitWorkerMgr(); err != nil {
		goto ERR
	}

	if err = master.InitLogMgr(); err != nil {
		goto ERR
	}

	// Start job mgr
	if err = master.InitJobMgr(); err != nil {
		goto ERR
	}
	// start http server
	if err = master.InitApiServer(); err != nil {
		goto ERR
	}

	select {}
	return
ERR:
	fmt.Println(err)
}
